package br.ufpe.cin.imlm2.jbeat.model.factory;

import java.util.ArrayList;
import java.util.List;

import br.ufpe.cin.imlm2.jbeat.model.LightWeightClass;
import br.ufpe.cin.imlm2.jbeat.model.LightWeightReferenceType;
import br.ufpe.cin.imlm2.util.ArgumentsValidator;

public class ClassMaker {

	private List<LightWeightClass> classes;
	private TypeMaker maker;
	
	public ClassMaker(TypeMaker tm) {
		ArgumentsValidator.verifyNullness(tm);
		this.classes = new ArrayList<LightWeightClass>();
		this.maker = tm;
		
	}
	
	/**
	 * Retrieves a <code>LightWeigthClass</code> with name <code>fullyQualifiedName</code>.
	 * If it doesn't exist a new one is created including the necessary type if needed.
	 * @param fullyQualifiedName
	 * @return
	 */
	public LightWeightClass retrieveClass(String fullyQualifiedName){
		LightWeightClass c;
		if((c = this.getLightWeightClass(fullyQualifiedName)) == null){
			LightWeightReferenceType type = (LightWeightReferenceType) this.maker.
				retrieveReferenceType(fullyQualifiedName, null);
			c = new LightWeightClass(type);
			type.setThisClass(c);
			this.classes.add(c);
		}
		return c;
	}
	
	private LightWeightClass getLightWeightClass(String fullyQualifiedName){
		for (LightWeightClass c : this.classes) {
			if(c.getFullyQualifiedName().equals(fullyQualifiedName))
				return c;
		}
		return null;
	}
	
	/**
	 * Retrieves all classes ever created by this maker.
	 * @return
	 */
	public List<LightWeightClass> retrieveAllClasses(){
		return this.classes;
	}
}
